/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.qframe.client.util;

import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.URL;
import org.qframe.client.render.IContentLoadCallBack;

/**
 *
 * @author gprasad
 */
public class ContentHelper {

    public static void load(String resource, final IContentLoadCallBack callback, final Object payload) {
        String fullUrl = URLUtil.contextToCompletePath(resource);
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(fullUrl));
        try {
            builder.sendRequest(null, new RequestCallback() {

                public void onResponseReceived(com.google.gwt.http.client.Request request, com.google.gwt.http.client.Response response) {
                    callback.onContentResponse(response.getText(), payload, false);
                }

                public void onError(com.google.gwt.http.client.Request request, Throwable exception) {
                    callback.onContentResponse(null, payload, true);
                }
            });
        } catch (RequestException ex) {
            throw new RuntimeException(ex);

        }
    }
}
